package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书...");

        // 1.判断书架是否满
        int size = bookList.getUsedSize();
        if(size == bookList.getBooks().length){
            System.out.println("书架已经满了 不能放入...");
            return;
        }

        // 2.构建对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要添加的书名： ");
        String name = scanner.nextLine();

        System.out.println("输入该书的作者： ");
        String author = scanner.nextLine();

        System.out.println("输入该书的类型： ");
        String type = scanner.nextLine();

        System.out.println("输入该书的价格： ");
        int price = scanner.nextInt();

        Book newbook = new Book(name,author,type,price);

        // 3.判断书架上是否已经有这本书
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                System.out.println("已经存在这本书 不能新增");
                System.out.println(book);
                return;
            }
        }

        // 4.插入这本书
        bookList.setBooks(size,newbook);

        bookList.setUsedSize(size + 1);

        System.out.println("已添加该书！");


    }
}
